package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.DiscardDefectReq;
import com.ruibang.glass.quality.domain.req.EverydayDiscardDefectReq;
import com.ruibang.glass.quality.entity.EverydayDiscardDefect;
import com.ruibang.glass.quality.mapper.EverydayDiscardDefectMapper;
import com.ruibang.glass.quality.service.EverydayDiscardDefectService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-08-30  10:56
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class EverydayDiscardDefectServiceImpl extends ServiceImpl<EverydayDiscardDefectMapper, EverydayDiscardDefect> implements EverydayDiscardDefectService {


    /**
     * @Description: 查询每日判废缺陷数量及曲线
     * @version v1.0
     * @author songJinKang
     * @date 2023-08-30 11:05
     */
    @Override
    public ResultBody queryEverydayDiscardDefect(EverydayDiscardDefectReq everydayDiscardDefectReq) {
        List<EverydayDiscardDefect> everydayDiscardDefectList = baseMapper.queryEverydayDiscardDefect(everydayDiscardDefectReq);
        everydayDiscardDefectList = everydayDiscardDefectList.stream().sorted(Comparator.comparing(EverydayDiscardDefect::getDiscardDate).reversed()).collect(Collectors.toList());
        return ResultBody.success(everydayDiscardDefectList);
    }

    @Override
    public void deleteByDate(DiscardDefectReq defectReq) {
        baseMapper.deleteByDate(defectReq);
    }


    @Override
    public ResultBody queryPageEveryHourColdDirect(EverydayDiscardDefectReq everydayDiscardDefectReq) {
        final IPage<EverydayDiscardDefect> page = new Page<>(everydayDiscardDefectReq.getPageNo(), everydayDiscardDefectReq.getPageSize());
        List<EverydayDiscardDefect> resps = baseMapper.queryPageEveryHourColdDirect(page, everydayDiscardDefectReq);
        if (CollectionUtils.isNotEmpty(resps)) {
            page.setRecords(resps);
        }
        return ResultBody.success(page);
    }

    @Override
    public List<EverydayDiscardDefect> getByDate(ArrayList<String> date) {
        QueryWrapper<EverydayDiscardDefect> hlQw = new QueryWrapper<>();
        hlQw.lambda().in(EverydayDiscardDefect::getDiscardDate, date);
        return this.list(hlQw);
    }
}
